<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
<%@ page import="com.example.demo2.DBUtil"%>
<%@ page import="java.util.HashMap" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("UTF-8");
		String condition = request.getParameter("condition");
		String user = request.getParameter("user");
		String pw = request.getParameter("pw");

		Connection cn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "";
		cn = DBUtil.getConnection();
		if(condition.equals("admin")){
			sql = "select * from admin where user = ? and psw = ? ";
			ps = cn.prepareStatement(sql);
			ps.setString(1, user);
			ps.setString(2, pw);
			rs = ps.executeQuery();
			if (rs.next()) {
				int id = rs.getInt("id");
				//设置最长访问间隔时间
				session.setMaxInactiveInterval(60*60*24);
				//将用户名存入session
				session.setAttribute("condition",condition);
				session.setAttribute("u_id",id);
				session.setAttribute("username",user);
				response.sendRedirect("admin.jsp");//登录成功，转到主
			}else{
				response.sendRedirect("denglu.jsp");//登录失败，重新登录
			}
		}else{
			sql = "select * from user where user = ? and psw = ? ";
			ps = cn.prepareStatement(sql);
			ps.setString(1, user);
			ps.setString(2, pw);
			rs = ps.executeQuery();
			if (rs.next()) {
				int id = rs.getInt("id");
				//设置最长访问间隔时间
				session.setMaxInactiveInterval(60*60*24);
				//将用户名存入session
				session.setAttribute("condition",condition);
				session.setAttribute("u_id",id);
				session.setAttribute("username",user);

				response.sendRedirect("shouye.jsp");//登录成功，转到主
			}else{
				response.sendRedirect("denglu.jsp");//登录失败，重新登录
			}
		}

	%>			
</body>
</html>